var toke = localStorage.getItem("key");
console.log(toke);

var allPrice = 0;

function addAll(num) {
	allPrice += num;
}

$.ajax({
	type: "get",
	url: "/api/shopcar/myShopCar",
	data: {
		"token": toke
	},
	async: true,
	dataType: "json",
	success: function(data) {
		var list = data.data.data;
		console.log(list);
		var str = '';
		for(var i = 0; i < list.length; i++) {
			str += '<ul class="item-content clearfix">' +
				'<li class="td td-chk">' +
				'<div class="cart-checkbox ">' +
				'<input class="check checkSingle" id="checkId' + i + '" name="items[]" value="170037950254" type="checkbox">' +
				'<label for="J_CheckBox_170037950254"></label>' +
				'</div>' +
				'</li>' +
				'<li class="td td-item">' +
				'<div class="item-pic">' +
				'<a href="introduction.html?id=' + list[i].goodsId + '" target="_blank" class="J_MakePoint">' +
				'<img src="' + list[i].imgUrl + '" class="itempic J_ItemImg">' +
				'</a>' +
				'</div>' +
				'<div class="item-info">' +
				'<div class="item-basic-info">' +
				'<a href="#" target="_blank" class="item-title J_MakePoint" data-point="tbcart.8.11">' + list[i].goodsName + '</a>' +
				'</div>' +
				'</div>' +
				'</li>' +
				'<li class="td td-info">' +
				'<div class="item-props item-props-can">' +
				'<span class="sku-line">规格：' + list[i].goodsStandard + '</span>' +
				'<span tabindex="0" class="btn-edit-sku theme-login">修改</span>' +
				'<i class="theme-login am-icon-sort-desc"></i>' +
				'</div>' +
				'</li>' +
				'<li class="td td-price">' +
				'<div class="item-price price-promo-promo">' +
				'<div class="price-content">' +
				'<div class="price-line">' +
				'￥<em class="J_Price price-now" tabindex="0">' + list[i].goodsPrice + '</em>' +
				'</div>' +
				'</div>' +
				'</div>' +
				'</li>' +
				'<li class="td td-amount">' +
				'<div class="amount-wrapper ">' +
				'<div class="item-amount ">' +
				'<div class="sl">' +
				'<input class="min am-btn" data-uid="' + i + '" name="" type="button" value="-" />' +
				'<input class="text_box singleKind" data-standard="' + 
					list[i].goodsStandard + '" data-goodid="' + list[i].goodsId + '" name="" type="text" value="' + 
					list[i].num + '"  data-img="' + list[i].imgUrl + '" style="width:30px;" />' +
				'<input class="add am-btn" data-uid="' + i + '" name="" type="button" value="+" />' +
				'</div>' +
				'</div>' +
				'</div>' +
				'</li>' +
				'<li class="td td-sum">' +
				'<div class="td-inner">' +
				'￥<em tabindex="0" data-unit=' + Number(list[i].goodsPrice) + ' data-goodtotal=' + 
					Number(list[i].goodsPrice * list[i].num) + ' id="uid' + i + '" class="J_ItemSum number">' + 
					Number(list[i].goodsPrice * list[i].num) + '</em>' +
				'</div>' +
				'</li>' +
				'<li class="td td-op">' +
				'<div class="td-inner">' +
				'<a href="javascript:;" data-point-url="#" class="delete">删除</a>' +
				'</div>' +
				'</li>' +
				'</ul>';
		}

		$(".bundle-main").html(str);

		$(".add").click(function() {
			var t = $(this).parent().find('input[class*=text_box]');
			t.val(Number(t.val()) + 1);

			var uid = $(this).data('uid');
			var uidEle = $('#uid' + uid);
			//前总价
			var uidPrice = Number($.trim(uidEle.text()));
			//单价
			var unitPrice = Number(uidEle.data('unit'));
			uidEle.text(uidPrice + unitPrice);
			uidEle.data('goodtotal', uidPrice + unitPrice);
			console.log(uidEle.data('goodtotal'));

			var checked = $('#checkId' + uid).attr('checked');
			var allPriceEle = null;
			var allPrice = 0;
			if(checked && checked == 'checked') {
				allPriceEle = $('#J_Total');
				allPrice = Number(allPriceEle.text());
				allPriceEle.text(allPrice + unitPrice)
				allPriceEle.data('totalprice', allPrice + unitPrice);
			}
		});

		$(".min").click(function() {
			var t = $(this).parent().find('input[class*=text_box]');
			t.val(Number(t.val()) - 1);
			if(Number(t.val()) < 0) {
				t.val(0);
				return;
			}
			var uid = $(this).data('uid');
			var uidEle = $('#uid' + uid);
			//前总价
			var uidPrice = Number($.trim(uidEle.text()));
			//单价
			var unitPrice = Number(uidEle.data('unit'));
			uidEle.text(uidPrice - unitPrice);
			uidEle.data('goodtotal', uidPrice - unitPrice);
			console.log(uidEle.data('goodtotal'));
			var checked = $('#checkId' + uid).attr('checked');
			var allPriceEle = null;
			var allPrice = 0;

			if(checked && checked == 'checked') {
				allPriceEle = $('#J_Total');
				allPrice = Number(allPriceEle.text());
				allPriceEle.text(allPrice - unitPrice);
				allPriceEle.data('totalprice', allPrice - unitPrice);
			}
		});

		$('.checkSingle').click(function() {
			var checkStatue = $(this).attr('checked');
			var J_Total = $('#J_Total');
			var totalprice = Number(J_Total.data('totalprice'));
			if(checkStatue && checkStatue == 'checked') {
				//选中
				var addTotal = Number($(this).parent().parent().parent().find('.J_ItemSum').data('goodtotal'));
				J_Total.data('totalprice', totalprice + addTotal);
				J_Total.text(totalprice + addTotal);
			} else {
				//反选
				var addTotal = Number($(this).parent().parent().parent().find('.J_ItemSum').data('goodtotal'));
				J_Total.data('totalprice', totalprice - addTotal);
				J_Total.text(totalprice - addTotal);
			}
		});

		$('#J_SelectAllCbx2').click(function() {
			var J_Total = $('#J_Total');
			var checkStatue = $(this).attr('checked');
			if(checkStatue && checkStatue == 'checked') {
				//全部选中
				$('.checkSingle').attr('checked', 'checked');
				var totalPrice = 0;
				$('.J_ItemSum').each(function() {
					totalPrice += Number($(this).data('goodtotal'));
				});
				J_Total.data('totalprice', totalPrice);
				J_Total.text(totalPrice);
			} else {
				//全部反选
				$('.checkSingle').removeAttr("checked");
				J_Total.data('totalprice', 0);
				J_Total.text(0);
			}
		});

		$('#J_Go').click(function() {
			var goods = [];
			$('.singleKind').each(function(index) {
				var checkSingle=$(this).parents('.item-content').eq(0).find('.checkSingle').eq(0);
				var checkStatue = checkSingle.attr('checked');
				if(checkStatue && checkStatue == 'checked') {
					goods.push({
						specification: $(this).data('standard'),
						goodsId: $(this).data('goodid'),
						num: parseInt($(this).val()),
						img: $(this).data('img'),
						price: $('#uid'+index).data('goodtotal')
					});
				}
			});
			localStorage.shopCarGoodsList=JSON.stringify(goods);
			window.location.href="pay.html"; 
			
			var data = {
				data: {
					goods: goods,
					token: "",
					addressId: "",
					remark: "",
					content: "",
					invoiceType: "0",
					invoiceTitle: "1",
					company: "",
					invoicePhone: "",
					invoiceEmail: "",
					invoiceContent: "1",
					tradetype: "0"
				}
			}
			console.log(data);
			data=JSON.stringify(data)
			console.log(data);
		});

	}
})